﻿using System;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltiProNet;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using MbUnit.Framework;

namespace Echo.Automation.Compliance.BusinessRules.WorkersComp
{
    [TestFixture]
    [Header("Config")]
    [Config(".NET")]
    [Author("Brent Crews", "Brent_Crews@ultimatesoftware.com")]
    [Category("Workers Compensation")]
    public class WorkersCompRiskRateGui : MbUnitAutomatedTest
    {
        UltiPro UltiPro;

        public UltiProDatabaseContext CompanyDatabaseDriver { get; set; }

        private double initial_eerate = 0.01;
        private double initial_errate = 0.02;
        private double initial_modifierrate = 1.5;
        private int effdtmonth = 2;
        private int effdtday = 15;
        private int effdtyear = 2008;

        private string wccode = "WCRR";
        private string wccoid = "49M7T";
        private string wcdesc = "Workers Comp Code Description";
        private string RRCompanyName = "US Payroll Company";

        private DateTime dtFrom;
        private DateTime dtTo;

        string dtFromString;
        string dtToString;

        string SqlQueryName1;
        string SqlQueryName2;

        [FixtureSetUp]
        public void Setup()
        {
            dtFrom = new DateTime(effdtyear, effdtmonth, effdtday);
            dtTo = new DateTime(effdtyear, effdtmonth, effdtday);

            dtFromString = dtFrom.Month.ToString() + "/" + dtFrom.Day.ToString() + "/" + dtFrom.Year.ToString();
            dtToString = dtTo.Month.ToString() + "/" + dtTo.Day.ToString() + "/" + dtTo.Year.ToString();
            CompanyDatabaseDriver = Init<UltiProDatabaseContext>();
            CompanyDatabaseDriver.Company = ConfigDatabase("ULTIPRO_CALENDAR");

            SqlQueryName1 = @"begin transaction IF NOT EXISTS (select * from ultipro_calendar.dbo.Codes where CodCode = 'WCRR' and CodTable = 'WCCODES' and CodCountryCode = 'USA') begin INSERT INTO Codes (CodCode ,CodDesc ,CodTable ,CodCountryCode )  VALUES ('" + wccode + "', '" + wcdesc + "', 'WCCODES', 'USA') end commit";
            CompanyDatabaseDriver.Company.Execute(SqlQueryName1);

            SqlQueryName2 = @"begin transaction IF NOT EXISTS (select * from WCRisk where WcrCoID = '49M7T' and WcrEffDate = '20080215 00:00:00.000') begin INSERT INTO WCRisk(WcrCoID, WcrDatetimeCreated, WcrEERiskRate, WcrEffDate, WcrEffStopDate, WcrERRiskRate, WcrHasBeenReplaced, WcrState, WcrSystemID,  WcrWCCode, WcrWcModifier) values('" + wccoid + "',GetDate(), " + initial_eerate.ToString() + ", '20080215 00:00:00.000', NULL, " + initial_errate.ToString() + ", 'N', 'FL', dbo.fn_GetTimedKey(), '" + wccode + "', " + initial_modifierrate.ToString() + ") end commit";
            this.CompanyDatabaseDriver.Company.Execute(SqlQueryName2);

            UltiPro = Launch<UltiPro>();
            UltiPro.Pages.Login.LoginAs("usa-canu", "password", Config.LanguageCode);

            UltiPro.Menu.SystemConfiguration.Click(menuItem => menuItem.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(fnd => fnd.BusinessRulesNameFilter, FilterType.StartsWith, "Workers' Compensation Risk Rates");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(0).Click(frstRw => frstRw.BusinessRulesLink);
            repeatClickOnPrevGrid();
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            UltiPro.Pages.WcRiskAddChg.Set(setTo => setTo.EmployeeRateTextBox, (initial_eerate * 100).ToString());
            UltiPro.Pages.WcRiskAddChg.Set(setTo => setTo.EmployerRateTextBox, (initial_errate * 100).ToString());
            UltiPro.Pages.WcRiskAddChg.Set(setTo => setTo.ModifierTextBox, initial_modifierrate.ToString());

            UltiPro.Pages.WcRiskAddChg.NewEffectiveDate.Set(dtFrom);

            UltiPro.Pages.WcRiskAddChg.ButtonBar.Click(link => link.Save);

            UltiPro.Browser.Quit();
        }

        [Test(Order = 1)]
        public void InsertedRow_VerifyWCRiskRatePgElements()
        {
            verifyItemsOnPage(initial_eerate, initial_errate, initial_modifierrate);
        }

        [Test(Order = 2)]
        [Row(0.03, 0.04, 2.5)]
        [Row(0.08, 0.02, 6.5)]
        [Row(0.01, 0.10, 7.5)]
        public void SavedRow_VerifyWCRiskRatePgElements(double new_eerate, double new_errate, double new_modifierrate)
        {
            UltiPro.Pages.WcRiskAddChg.Set(setTo => setTo.EmployeeRateTextBox, (new_eerate * 100).ToString());
            UltiPro.Pages.WcRiskAddChg.Set(setTo => setTo.EmployerRateTextBox, (new_errate * 100).ToString());
            UltiPro.Pages.WcRiskAddChg.Set(setTo => setTo.ModifierTextBox, new_modifierrate.ToString());

            UltiPro.Pages.WcRiskAddChg.NewEffectiveDate.Set(dtFrom);

            UltiPro.Pages.WcRiskAddChg.ButtonBar.Click(link => link.Save);

            repeatClickOnPrevGrid();

            verifyItemsOnPage(new_eerate, new_errate, new_modifierrate);
        }

        #region Helper methods
        private void repeatClickOnPrevGrid()
        {
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Set(frmDt => frmDt.FromDate, dtFromString);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Set(toDt => toDt.ToDate, dtToString);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Click(filtBtn => filtBtn.FilterButton);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Row(row => row.CodeLink, "WCRR").Click(frstRw => frstRw.CodeLink);
        }

        private void verifyItemsOnPage(double eerate, double errate, double modifierrate)
        {
            UltiPro.Pages.WcRiskAddChg.Is(tstIf => tstIf.CompanyValue, "innerHTML", RRCompanyName);
            UltiPro.Pages.WcRiskAddChg.Is(tstIf => tstIf.EmployeeRateTextBox, "value", (eerate * 100).ToString() + ".00 %");
            UltiPro.Pages.WcRiskAddChg.Is(tstIf => tstIf.EmployerRateTextBox, "value", (errate * 100).ToString() + ".00 %");
            UltiPro.Pages.WcRiskAddChg.Is(tstIf => tstIf.ModifierTextBox, "value", modifierrate.ToString() + "0");
            UltiPro.Pages.WcRiskAddChg.Is(tstIf => tstIf.TotalRateTextBox, "innerHTML", ((eerate * 100) + ((errate * 100) * modifierrate)).ToString() + ".00");
        }
        #endregion
    }
}
